<template>
  <div class="product-detail">
    <div v-if="product" class="detail-container">
      <div class="product-image">
        <img :src="product.image" :alt="product.name" />
      </div>
      <div class="product-info">
        <h1 class="product-title">{{ product.name }}</h1>
        <p class="product-price">¥{{ product.price.toLocaleString() }}</p>
        <p class="product-description">{{ product.description }}</p>
        
        <div class="product-meta">
          <p><strong>分类：</strong>{{ product.category }}</p>
          <p><strong>库存：</strong>{{ product.stock }} 件</p>
        </div>
        
        <div class="action-buttons">
          <button 
            class="add-to-cart-btn" 
            @click="addToCart"
            :disabled="product.stock === 0"
          >
            {{ product.stock === 0 ? '缺货' : '加入购物车' }}
          </button>
          <button class="back-btn" @click="goBack">返回商品列表</button>
        </div>
      </div>
    </div>
    <div v-else class="not-found">
      <h2>商品未找到</h2>
      <button class="back-btn" @click="goBack">返回商品列表</button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useProductsStore } from '../stores/products'
import { useCartStore } from '../stores/cart'
import { useUserStore } from '../stores/user'

const route = useRoute()
const router = useRouter()
const productsStore = useProductsStore()
const cartStore = useCartStore()
const userStore = useUserStore()

const product = computed(() => {
  console.log('Trying to get product with id:', route.params.id)
  return productsStore.getProductById(route.params.id)
})

const addToCart = () => {
  if (!userStore.isLoggedIn) {
    if (confirm('请先登录后再加入购物车\n\n是否前往登录页面？')) {
      router.push('/user')
    }
    return
  }
  
  if (product.value) {
    cartStore.addToCart(product.value)
    alert(`${product.value.name} 已加入购物车！`)
  }
}

const goBack = () => {
  router.push('/')
}
</script>

<style scoped>
.product-detail {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
  min-height: 80vh;
}

.detail-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  align-items: start;
}

.product-image img {
  width: 100%;
  max-width: 500px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.product-info {
  padding: 20px;
}

.product-title {
  font-size: 2rem;
  font-weight: bold;
  margin-bottom: 16px;
  color: #333;
}

.product-price {
  font-size: 2.5rem;
  font-weight: bold;
  color: #e74c3c;
  margin-bottom: 20px;
}

.product-description {
  font-size: 1.1rem;
  line-height: 1.6;
  color: #666;
  margin-bottom: 24px;
}

.product-meta {
  margin-bottom: 32px;
}

.product-meta p {
  margin: 8px 0;
  font-size: 1rem;
  color: #555;
}

.action-buttons {
  display: flex;
  gap: 16px;
}

.add-to-cart-btn {
  flex: 1;
  padding: 16px 24px;
  background-color: #007acc;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.add-to-cart-btn:hover:not(:disabled) {
  background-color: #005c99;
}

.add-to-cart-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.back-btn {
  padding: 16px 24px;
  background-color: #6c757d;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: background-color 0.2s ease;
}

.back-btn:hover {
  background-color: #5a6268;
}

.not-found {
  text-align: center;
  padding: 80px 20px;
}

.not-found h2 {
  font-size: 2rem;
  color: #666;
  margin-bottom: 24px;
}

@media (max-width: 768px) {
  .detail-container {
    grid-template-columns: 1fr;
    gap: 24px;
  }
  
  .product-title {
    font-size: 1.5rem;
  }
  
  .product-price {
    font-size: 2rem;
  }
  
  .action-buttons {
    flex-direction: column;
  }
}
</style> 